package org.idea.spring.common.error.controller;

import org.idea.spring.common.error.service.impl.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Lookup;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @Author linhao
 * @Date created in 6:27 下午 2022/8/3
 */
@RestController
public class TestController {

    //    @Autowired //注意使用autowired注解引入的对象一定是一个单例
//    private TestService testService;
    @Resource
    private TestService testService;
    @Autowired
    private ApplicationContext applicationContext;

    @GetMapping(value = "/do-test")
    public String doTest() {
        System.out.println("hello testService is " + testService);
        System.out.println("hello getTestService is " + getTestService());
        System.out.println("hello getTestService2 is " + getTestService2());
        return "success";
    }

    //这样子每次都会是不同的对象
    public TestService getTestService() {
        return applicationContext.getBean(TestService.class);
    }

    @Lookup
    public TestService getTestService2(){
        System.out.println("get test service 2");
        return null;
    }

}
